package com.cskaoyan.service;

import com.cskaoyan.bean.Role;
import com.cskaoyan.bean.RoleExample;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.vo.RoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{
    @Autowired
    RoleMapper roleMapper;


    @Override
    public RoleVo list(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);

        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(sort + " " + order);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        long total = pageInfo.getTotal();
        RoleVo roleVo = new RoleVo();
        roleVo.setTotal(total);
        roleVo.setItems(roles);

        return roleVo;
    }

    @Override
    public RoleVo list(Integer page, Integer limit, String sort, String order, String name) {
        PageHelper.startPage(page,limit);

        RoleExample roleExample = new RoleExample();

        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameEqualTo(name);
        roleExample.setOrderByClause(sort + " " + order);
        List<Role> roles = roleMapper.selectByExample(roleExample);

        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        long total = pageInfo.getTotal();
        RoleVo roleVo = new RoleVo();
        roleVo.setTotal(total);
        roleVo.setItems(roles);

        return roleVo;

    }

    @Override
    public void update(Role newrole) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdEqualTo(newrole.getId());
        roleMapper.updateByExample(newrole,roleExample);
    }

    @Override
    public void delete(Role role) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdEqualTo(role.getId());
        roleMapper.deleteByExample(roleExample);

    }

    @Override
    public  Role create(Role role) {
         roleMapper.insert(role);
        return role;
    }


}
